<template>
  <div class="addcode">
    <el-dialog
      title="添加礼包码"
      :close-on-click-modal="false"
      :visible.sync="isVisible"
      width="30%"
      @close="closeDialog"
      center
    >
      <el-form
        :model="formdata"
        :rules="rules"
        ref="formdata"
        class="demo-ruleForm"
      >
        <el-form-item prop="code">
          <el-input
            type="textarea"
            placeholder="每行一个礼包码，请保证礼包码输入正确"
            v-model="formdata.code"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('formdata')"
          >确 定</el-button
        >
        <el-button @click="isVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { editGift } from "@/api/giftManage";
export default {
  inject: ["reload"],
  data() {
    return {
      isVisible: this.isShow,
      formdata: {
        code: "",
        gameGifid: this.dialogRow.id,
      },
      rules: {
        code: [{ required: true, message: "请输入礼包码", trigger: "blur" }],
      },
    };
  },
  props: {
    isShow: Boolean,
    dialogRow: Object,
  },
  methods: {
    closeDialog() {
      this.$emit("closeDialog");
    },
    // 表单提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const para = Object.assign({}, this.formdata);
          var str = this.formdata.code;
          str = str.replace(/\n/g, ",");
          var strArr = str.split(",");
          para.code = str;
          var codeFlag = true;
          strArr.some((value, index) => {
            if (value.length == 0) {
              this.$message({
                message: "检测礼包码里含有空行，请检查后再添加",
                type: "warning",
              });
              codeFlag = false;
              return true;
            }
          });
          if (codeFlag == true) {
            editGift(para)
              .then((res) => {
                this.$message({
                  message: "补仓成功！",
                  type: "success",
                });
                setTimeout(() => {
                  this.reload();
                }, 20);
              })
              .catch((error) => {});
            this.isVisible = false;
          }
        }
      });
    },
  },
};
</script>
<style lang="scss">
.addcode {
  .el-textarea__inner {
    height: 304px;
  }
  .el-textarea__inner::placeholder {
    font-size: 12px;
  }
  .el-dialog__wrapper .el-input.is-disabled .el-input__inner {
    color: #a5a4bf !important;
  }
  .el-textarea__inner,
  .el-textarea__inner::placeholder {
    color: #a5a4bf;
  }
  .el-textarea__inner {
    resize: none;
  }
}
</style>